package org.kwisatz.proxybot.filters;

import java.util.Random;

import org.kwisatz.proxybot.FilterAdapter;
import org.kwisatz.proxybot.Message;

public class Captcha extends FilterAdapter
{
	private static final Random random=new Random();
	private final static String CMD="!captcha";
	public String handleClientLine(String line, Message message)
	{
		String eom=message.getEndOfMsg();
		if(eom!=null && eom.startsWith(CMD) &&
			Message.PRIVMSG.equals(message.getCommand()))
		{
			String chan=message.getCmdParam();
			String prefix= ':' +context.getNickName()+
				"!proxybot@localhost PRIVMSG "+chan+" :";
			
			String s=filter(eom.substring(CMD.length()));
			context.getClient().println(prefix+s);
			return Message.PRIVMSG+ ' ' +chan+" :"+s;
		}

		return line;
	}

	private static String filter(String line)
	{
		StringBuilder sb=new StringBuilder();
		
		for(int i=0; i<line.length()-1; i++)
		{
			sb.append(line.charAt(i));
			if(random.nextBoolean())				
				sb.append('`');
			else
				sb.append(' ');
		}
		sb.append(line.charAt(line.length()-1));
		return sb.toString();
	}
	
	public static void main(String[] args)
	{
		System.out.println(filter("http://forbidden"));
		System.out.println(filter("Some words I want to say"));
	}
}
